﻿Imports Windows.Storage
Imports Windows.Media.MediaProperties
Imports Windows.Media.Transcoding
Imports LSW.Utility

Namespace Media
    Public Module TransCoder
        Public Event Progress(asyncInfo As IAsyncActionWithProgress(Of Double), percent As Double)
        Public Event Complete(asyncInfo As IAsyncActionWithProgress(Of Double), status As AsyncStatus)

        Public Async Function TranscodeMP4(src As StorageFile, dest As StorageFile, Optional encode As VideoEncodingQuality = VideoEncodingQuality.HD720p) As Task(Of String)
            Dim profile = MediaEncodingProfile.CreateMp4(encode)
            Dim trans As New MediaTranscoder
            Dim prepareOp = Await trans.PrepareFileTranscodeAsync(src, dest, profile)
            If prepareOp.CanTranscode Then
                Dim transcodeOp = prepareOp.TranscodeAsync
                transcodeOp.Progress = New AsyncActionProgressHandler(Of Double)(AddressOf TranscodeProgress)
                transcodeOp.Completed = New AsyncActionWithProgressCompletedHandler(Of Double)(AddressOf TranscodeComplete)
                Return " 正在转码。。。 "
            Else
                Select Case prepareOp.FailureReason
                    Case TranscodeFailureReason.CodecNotFound
                        Return " 未找到解码器！ "
                    Case TranscodeFailureReason.InvalidProfile
                        Return " 无效的编码配置文件！ "
                    Case Else
                        Return " 未知错误！ "
                End Select
            End If
        End Function

        Public Async Function TranscodeMP4(src As StorageFile, dest As StorageFile, startTime As TimeSpan, endTime As TimeSpan, Optional encode As VideoEncodingQuality = VideoEncodingQuality.HD720p) As Task(Of String)
            Dim profile = MediaEncodingProfile.CreateMp4(encode)
            Dim trans As New MediaTranscoder
            trans.TrimStartTime = startTime
            trans.TrimStopTime = endTime
            Dim prepareOp = Await trans.PrepareFileTranscodeAsync(src, dest, profile)
            If prepareOp.CanTranscode Then
                Dim transcodeOp = prepareOp.TranscodeAsync
                transcodeOp.Progress = New AsyncActionProgressHandler(Of Double)(AddressOf TranscodeProgress)
                transcodeOp.Completed = New AsyncActionWithProgressCompletedHandler(Of Double)(AddressOf TranscodeComplete)
                Return " 正在转码。。。 "
            Else
                Select Case prepareOp.FailureReason
                    Case TranscodeFailureReason.CodecNotFound
                        Return " 未找到解码器！ "
                    Case TranscodeFailureReason.InvalidProfile
                        Return " 无效的编码配置文件！ "
                    Case Else
                        Return " 未知错误！ "
                End Select
            End If
        End Function

        Private Sub TranscodeProgress(asyncInfo As IAsyncActionWithProgress(Of Double), percent As Double)
            RaiseEvent Progress(asyncInfo, percent)
        End Sub

        Private Sub TranscodeComplete(asyncInfo As IAsyncActionWithProgress(Of Double), status As AsyncStatus)
            RaiseEvent Complete(asyncInfo, status)
        End Sub

        Public Async Sub TranscodeVideoFile()
            Dim src, dest As StorageFile
            src = Await UIHelper.ShowVideoSelecter
            dest = Await UIHelper.ShowSaveFileDialog(".mp4")
            Await TranscodeMP4(src, dest)
        End Sub
    End Module
End Namespace